package com.itheima.controller.user;


import com.itheima.common.constant.MessageConstant;
import com.itheima.common.constant.RedisMessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.SMSUtils;
import com.itheima.common.utils.ValidateCodeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/validatecode")
@Api("发送验证码")
@Slf4j
public class UserValidatecodeController {

    @Autowired
    private RedisTemplate redisTemplate;

    //发送验证码
    @PostMapping("/send4Order")
    @ApiOperation("发送验证码")
    public Result send4order(String telephone) {

        //生成固定验证码1234
        String validateCode = "1234";
        String key = telephone + RedisMessageConstant.SENDTYPE_ORDER;
        log.info("验证码：{}", validateCode);

        //将验证码储存到redis中（5min）
        redisTemplate.opsForValue().set(
                key,
                validateCode,
                5, TimeUnit.MINUTES);


        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS, validateCode);
    }
    //发送登录验证码
    @GetMapping ("/send4Login")
    @ApiOperation("发送验证码")
    public Result send4Login(String telephone) {

        //生成固定验证码1234
        String validateCode = "1234";
        String key = telephone + RedisMessageConstant.SENDTYPE_LOGIN;
        log.info("验证码：{}", validateCode);

        //将验证码储存到redis中（5min）
        redisTemplate.opsForValue().set(
                key,
                validateCode,
                5, TimeUnit.MINUTES);


        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS, validateCode);
    }
}
